package com.cy.pj.sys.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cy.pj.common.annotation.RequiredLog;
import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.vo.PageObject;
import com.cy.pj.sys.dao.SysUserDao;
import com.cy.pj.sys.dao.SysUserRoleDao;
import com.cy.pj.sys.entity.SysUser;
import com.cy.pj.sys.service.SysUserService;
import com.cy.pj.sys.vo.SysUserDeptVo;

import io.micrometer.core.instrument.util.StringUtils;

@Service//会创建此对象 并将对象存储到map容器 spring Bean容器中的key默认类名首字母小写
public class SysUserServiceImpl implements SysUserService{

	@Autowired
	private SysUserDao dao;
	@Autowired
	private SysUserRoleDao surd;
	@RequiredLog
	@Override
	public PageObject<SysUserDeptVo> findPageObjects(String username, Integer pageCurrent) {
		//1.对参数进行效验
		if(pageCurrent==null||pageCurrent<1)
			throw new IllegalArgumentException("当前页码值无效");
		//2.查询总记录数并进行效验
		int rowCount = dao.getRowCount(username);
		if(rowCount==0) {
			throw new ServiceException("没有找到对应记录");
		}
		//3.查询当前页记录
		int pageSize=3;
		int startIndex=(pageCurrent-1)*pageSize;
		List<SysUserDeptVo> findPageObjects = dao.findPageObjects(username, startIndex, pageSize);
		//4.对查询结果进行封装并返回
		return new PageObject<>(pageCurrent, pageSize, rowCount,findPageObjects);
	}

	@Override
	public int validById(Integer id, Integer valid, String modifiedUser) {
		//1.验证合法性
		if(id==null||id<0) {
			throw new ServiceException("参数不和法,id="+id);
		}
		if(valid!=1&&valid!=0) {
			throw new ServiceException("参数不合法,valid="+valid);
		}
		if(StringUtils.isEmpty(modifiedUser)) {
			throw new ServiceException("修改用户不能为空");
		}
		//2.执行禁用或启动操作
		int rows = dao.validById(id, valid, modifiedUser);
		if(rows==0) {
			throw new ServiceException("此记录可能已经不存在");
		}
		return rows;
	}

	@Override
	public int saveObject(SysUser entity, Integer[] roleIds) {
		//1.参数效验
		if(entity==null) {
			throw new ServiceException("保存对象不能为空");
		}
		if(StringUtils.isEmpty(entity.getUsername())) {
			throw new ServiceException("用户名不能为空");
		}
		if(StringUtils.isEmpty(entity.getPassword())) {
			throw new ServiceException("密码不能为空");
		}
		if(roleIds==null||roleIds.length==0) {
			throw new ServiceException("至少要为用户分配角色");
		}
		//2.保存用户自身信息 
		//2.1对密码进行加密
		String source=entity.getPassword();
		//获取一个盐值 这个值使用随机的字符串
		String salt=UUID.randomUUID().toString();
		//借助shiro框架中的API对应密码
		SimpleHash sh = new SimpleHash(
				"MD5",
				source,
				salt,
				1);
		//将加密结果转换为16进制
		String pwd = sh.toHex();
		entity.setSalt(salt);
		entity.setPassword(pwd);
		//持久化到数据库
		int rows = dao.insertObject(entity);
		surd.insertObjects(entity.getId(), roleIds);
		//返回结果
		return rows;
	}

	@Override
	public Map<String, Object> findObjectById(Integer userId) {
		//1.合法性验证
		if(userId==null||userId<=0) {
			throw new ServiceException("参数数据不合法 userId="+userId);
		}
		//2.业务查询
		SysUserDeptVo user = dao.findObjectById(userId);
		if(user==null)
			throw new ServiceException("此用户已经不存在");
		List<Integer> roleIds = surd.findRoleIdsByUserId(userId);
		//3.封装数据
		Map<String,Object> map=new HashMap<>();
		map.put("user", user);
		map.put("roleIds", roleIds);
		return map;
	}

	@Override
	public int updateObject(SysUser entity, Integer[] roleIds) {
		//1.参数有效性验证
		if(entity==null) {
			throw new IllegalArgumentException("保存对象不能为空");
		}
		if(StringUtils.isEmpty(entity.getUsername()))
			throw new IllegalArgumentException("用户名不能为空");
		if(roleIds==null||roleIds.length==0)
			throw new IllegalArgumentException("必须为其指定角色");
		//其他验证自己实现 例如用户名已经存在 密码长度
		//2.更新用户自身信息
		int rows = dao.updateObject(entity);
		surd.deleteObjectsByUserId(entity.getId());
		surd.insertObjects(entity.getId(), roleIds);
		return rows;
	}
	
}

